﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Bonehead
{
    public class ParsedProfile
    {       
        private Dictionary<long, string> mMethodMap = new Dictionary<long, string>();
        private Dictionary<long, ParsedThread> mThreads = new Dictionary<long, ParsedThread>();

        public ParsedProfile(Profile aProfile)
        {
            ParsedThread thread = null;
            foreach (Profile.Method method in aProfile.Methods)
            {
                mMethodMap.Add(method.Function, method.Name);
            }

            foreach (Profile.Sample sample in aProfile.Samples)
            {
                if (thread == null || thread.ID != sample.Thread)
                {
                    if (!mThreads.TryGetValue(sample.Thread, out thread))
                    {
                        thread = new ParsedThread(sample.Thread, this);
                        mThreads.Add(sample.Thread, thread);
                    }
                }
                thread.Sample(sample);
            }
        }

        public IEnumerable<ParsedThread> Threads
        {
            get { return mThreads.Values; }
        }

        public string GetMethodName(long aFunctionID)
        {
            string name;
            if (mMethodMap.TryGetValue(aFunctionID, out name))
            {
                return name;
            }
            return String.Empty;
        }
    }
}
